/**
 * Filename: AlertNameActivity.java
 * @author  Firefly
 * @contact postfirefly@163.com
 * @time    2016/3/18 16:08
 */
package com.github.postsanf.yinian.activity;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.BaseApplication;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.ISkinUpdate;
import com.github.postsanf.yinian.utils.SkinPackageManager;
import com.umeng.analytics.MobclickAgent;

/**
 * 修改名称
 */
public class RemarkChatNameActivity extends BaseActivity implements View.OnClickListener,ISkinUpdate {
    private Toolbar                 mToolbar;
    private EditText                et_alert_name_code;
    private ImageView               iv_alert_name_delete;
    private String					mName;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yn_activity_alert_name);
        BaseApplication.getInstance().mActivitys.add(this);
        mName	=  getIntent().getExtras().getString(CommonConstants.YNDATAS,"");
        initView();
    }

    private void  initView() {
        mToolbar = (Toolbar) findViewById(R.id.alert_name_toolbar);
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(R.drawable.ic_return);

        et_alert_name_code   = (EditText) findViewById(R.id.et_alert_name_code);
        iv_alert_name_delete = (ImageView) findViewById(R.id.iv_alert_name_delete);

        et_alert_name_code.setText(mName);
        et_alert_name_code.setSelection(mName.length());
        iv_alert_name_delete.setOnClickListener(this);
        et_alert_name_code.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() <= 0) {
                    iv_alert_name_delete.setVisibility(View.GONE);
                } else {
                    iv_alert_name_delete.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_alert_name_delete:
                et_alert_name_code.setText("");
                break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_advice, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            case R.id.action_submit:
                String name = et_alert_name_code.getText().toString().trim();
                if(name.length()==0){
                    showToast("名称不能为空");
                    return true;
                }else if(mName.equals(name)){
                    finish();
                }



                Intent intent=new Intent();
                intent.putExtra(CommonConstants.REQ_CODE, 1);
                intent.putExtra(CommonConstants.REQ_DATA, name);
                setResult(RESULT_OK, intent);
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void updateTheme() {
        if(mToolbar!=null){
            try {
                Resources mResource= SkinPackageManager.getInstance(this).mResources;
                mToolbar.setNavigationIcon(mResource.getDrawable(mResource.getIdentifier("ic_return", "drawable", "com.github.postsanf.skinby")));
                mToolbar.setBackgroundColor(mResource.getColor(mResource.getIdentifier("yncolor", "color", "com.github.postsanf.skinby")));
                mToolbar.setTitleTextColor(mResource.getColor(mResource.getIdentifier("border_color", "color", "com.github.postsanf.skinby")));
            } catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BaseApplication.getInstance().mActivitys.remove(this);
    }

    // 友盟统计
    public void onResume(){
        super.onResume();
        if(SkinPackageManager.getInstance(this).mResources!=null){
            updateTheme();
        }
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause(){
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}
